<?php

namespace App\Ability;


use Hyperf\Validation\Validator;

trait ApiResponse
{
    /**
     * 成功的响应
     * @param array $data
     * @param string $message
     * @return array
     */
    public function success(array $data = [] ,string $message = '')
    {
        return [
            "code" => 0 ,
            "msg" => $message,
            "data" => $data,
        ];
    }

    /**
     * 出错的响应
     * @param int $code
     * @param string $message
     * @param array $data
     * @return array
     */
    public function error(int $code , string $message = '', array $data = [] )
    {
        return [
            "code" => $code ,
            "msg" => $message,
            "data" => $data,
        ];
    }

    /**
     * 验证失败
     * @param Validator $validator
     * @return array
     */
    public function fails(Validator $validator)
    {
        return [
            "code" => 100,
            "msg" => "validate error !",
            "data" => [
                "errors" => $validator->errors()
            ],
        ];
    }
}